package tables

import (
	"app/models"
	"app/utils"

	"github.com/GoAdminGroup/go-admin/context"
	"github.com/GoAdminGroup/go-admin/modules/db"
	form2 "github.com/GoAdminGroup/go-admin/plugins/admin/modules/form"
	"github.com/GoAdminGroup/go-admin/plugins/admin/modules/table"
	"github.com/GoAdminGroup/go-admin/template/types"
	"github.com/GoAdminGroup/go-admin/template/types/form"
	editType "github.com/GoAdminGroup/go-admin/template/types/table"
)

func GetGreenhouseTable(ctx *context.Context) table.Table {

	greenhouse := table.NewDefaultTable(table.DefaultConfigWithDriver("mysql"))

	info := greenhouse.GetInfo()
	info.SetFilterFormLayout(form.LayoutTwoCol)
	info.AddField("Id", "id", db.Int).
		FieldFilterable().
		FieldSortable()
	info.AddField("大棚名称", "name", db.Varchar).
		FieldFilterable().
		FieldSortable().
		FieldEditAble()
	info.AddField("备注", "remark", db.Varchar).
		FieldFilterable().
		FieldSortable().
		FieldEditAble()
	info.AddField("大棚管理员", "username", db.Varchar).
		FieldJoin(types.Join{
			Table:     "goadmin_users",
			JoinField: "id",
			Field:     "manager_id",
		}).FieldFilterable(types.FilterType{FormType: form.SelectSingle}).
		FieldFilterOptionsFromTable("goadmin_users", "username", "username", func(model *db.SQL) *db.SQL {
			return model.Where("id", ">", 2)
		}).
		FieldSortable()

	info.AddField("是否启用", "is_active", db.Tinyint).
		FieldDisplay(func(model types.FieldModel) interface{} {
			switch model.Value {
			case "1": // 启用
				return "启用"
			case "0": // 弃用
				return "弃用"
			default: // 未知
				return "未知"
			}
		}).
		FieldFilterable(types.FilterType{FormType: form.SelectSingle}).
		FieldFilterOptions(types.FieldOptions{
			{Text: "启用", Value: "1"},
			{Text: "弃用", Value: "0"},
		}).
		FieldEditAble(editType.Select).FieldEditOptions(types.FieldOptions{
		{Text: "启用", Value: "1"},
		{Text: "弃用", Value: "0"},
	}).
		FieldSortable()
	info.AddField("创建时间", "created_at", db.Timestamp).
		FieldSortable()
	info.AddField("修改时间", "updated_at", db.Timestamp).
		FieldSortable()

	info.SetTable("greenhouse").SetTitle("大棚管理").SetDescription("Greenhouse")

	formList := greenhouse.GetForm()
	formList.AddField("Id", "id", db.Int, form.Default).
		FieldDisableWhenCreate().
		FieldDisableWhenUpdate()
	formList.AddField("大棚名称", "name", db.Varchar, form.Text)
	formList.AddField("备注", "remark", db.Varchar, form.Text)
	formList.AddField("管理员", "manager_id", db.Int, form.SelectSingle).FieldOptionsFromTable("goadmin_users", "username", "id", func(model *db.SQL) *db.SQL {
		return model.Where("id", ">", 2)
	}).FieldHelpMsg("请选择大棚管理员")
	formList.AddField("是否启用", "is_active", db.Tinyint, form.Radio).
		// radio的选项，Text代表显示内容，Value代表对应值
		FieldOptions(types.FieldOptions{
			{Text: "启用", Value: "1"},
			{Text: "弃用", Value: "0"},
		}).FieldDefault("1")
	formList.AddField("创建时间", "created_at", db.Timestamp, form.Datetime).
		FieldDisableWhenUpdate().
		FieldHide().FieldNowWhenInsert()
	formList.AddField("修改时间", "updated_at", db.Timestamp, form.Datetime).
		FieldDisableWhenCreate().
		FieldHide().FieldNowWhenUpdate()

	formList.SetTable("greenhouse").SetTitle("大棚管理").SetDescription("Greenhouse")

	formList.SetInsertFn(func(values form2.Values) error {
		// 模型实例
		o := &models.Greenhouse{}

		if values.Get("name") != "" {
			o.Name = values.Get("name")
		}
		if values.Get("remark") != "" {
			o.Remark = values.Get("remark")
		}
		if values.Get("manager_id") != "" {
			o.ManagerID = utils.ToInt(values.Get("manager_id"))
		}
		if values.Get("is_active") != "" {
			o.IsActive = utils.ToInt(values.Get("is_active"))
		}

		// 插入新的订单记录
		res := models.Orm.Create(o)
		return res.Error
	})

	formList.SetUpdateFn(func(values form2.Values) error {
		ID := values.Get("id")
		o := new(models.Greenhouse)
		res := models.Orm.First(o, ID)
		if res.Error != nil {
			return res.Error
		}

		if values.Get("name") != "" {
			o.Name = values.Get("name")
		}
		if values.Get("remark") != "" {
			o.Remark = values.Get("remark")
		}
		var updateManagerID int
		if values.Get("manager_id") != "" {
			if o.ManagerID != utils.ToInt(values.Get("manager_id")) {
				updateManagerID = utils.ToInt(values.Get("manager_id"))
			}
			o.ManagerID = updateManagerID
		}
		if values.Get("is_active") != "" {
			o.IsActive = utils.ToInt(values.Get("is_active"))
		}

		// 插入新的订单记录
		res = models.Orm.Save(o)
		if res.Error != nil {
			return res.Error
		}
		if updateManagerID > 0 {
			// 更新大棚分配记录
			res = models.Orm.Model(&models.GreenhouseAllocationRecord{}).Where("greenhouse_id = ?", o.ID).Update("manager_id", o.ManagerID)
			if res.Error != nil {
				return res.Error
			}
		}
		return nil
	})

	return greenhouse
}
